package com.spark.incoicings.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.dao.*;
import com.spark.incoicings.service.DeliveryOrderService;
import com.spark.incoicings.service.DictTypeService;
import com.spark.incoicings.vo.DictVO;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 没考虑性能
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class DeliveryController {
    @Resource
    private DictTypeService dictTypeService;
    @Resource
    private DeliveryOrderMapper deliveryOrderMapper;
    @Resource
    private DeliveryOrderService deliveryOrderService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ShipmentDetailMapper shipmentDetailMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private SalesOrderMapper salesOrderMapper;
    @GetMapping("/selectShipment")
    public Result selectShipment(@RequestParam String type){
        List<DictVO> dictVO = (List<DictVO>) redisTemplate.opsForValue().get(type);
        if (dictVO!=null){
            return Result.success(200,"success",dictVO);
        }
        List<DictVO> shipment = dictTypeService.selectShipment(type);
        redisTemplate.opsForValue().set(type,shipment,1, TimeUnit.HOURS);
        return Result.success(200,"success",shipment);
    }

    @GetMapping("/selectShippingAll")
    public Result selectShippingAll(){
        List<DictVO> selected = dictTypeService.selectShippingAll();
        return Result.success(200,"success",selected);
    }
    @GetMapping("/selectDeliveryOrderAll")
    public Result selectDeliveryOrderAll(@RequestParam(defaultValue = "1")Integer pageNum,
                                         @RequestParam(defaultValue = "3")Integer pageSize,
                                         @RequestParam(required = false)String customerName,
                                         @RequestParam(required = false)Integer status,
                                         @RequestParam(required = false)String orderNumber,
                                         @RequestParam(required = false)String outboundNumber,
                                         @RequestParam(required = false)String shipmentStatusId){
        PageHelper.startPage(pageNum,pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("customerName",customerName);
        map.put("status",status);
        map.put("orderNumber",orderNumber);
        map.put("outboundNumber",outboundNumber);
        map.put("shipmentStatusId",shipmentStatusId);
        List<DeliveryOrder> deliveryOrders = deliveryOrderService.selectDeliveryOrderAll(map);
        log.info("deliveryOrders={}",deliveryOrders);
        PageInfo<DeliveryOrder> pageInfo = new PageInfo<>(deliveryOrders);
        return Result.success(200,"success",pageInfo);
    }

    @GetMapping("/selectDeliveryOrderById")
    public Result selectDeliveryOrderById(Integer id){
        DeliveryOrder deliveryOrder = deliveryOrderMapper.selectById(id);
        List<ShipmentDetail> shipmentDetails = shipmentDetailMapper.selectList(new QueryWrapper<ShipmentDetail>()
                                               .eq("shipping_order_id", deliveryOrder.getDeliveryId()));
        shipmentDetails.forEach(shipmentDetail -> {
            Product product = productMapper.selectById(shipmentDetail.getProductId());
            shipmentDetail.setProduct(product);
            OrderDetail orderDetail = orderDetailMapper.selectById(shipmentDetail.getOrderDetailId());
            SalesOrder salesOrder = salesOrderMapper.selectById(orderDetail.getSalesOrderId());
            orderDetail.setSalesOrder(salesOrder);
            shipmentDetail.setOrderDetail(orderDetail);
        });
        deliveryOrder.setShipmentDetails(shipmentDetails);
        return Result.success(200,"success",deliveryOrder);
    }
    @PostMapping("/saveDeliveryOrder")
    public Result saveDeliveryOrder(@RequestBody DeliveryOrder deliveryOrder){
        log.info("deliveryOrder={}",deliveryOrder);
        deliveryOrderService.save(deliveryOrder);
        deliveryOrder.getShipmentDetails().forEach(shipmentDetail -> {
            shipmentDetail.setShippingOrderId(deliveryOrder.getDeliveryId());
            shipmentDetailMapper.insert(shipmentDetail);
        });
        return Result.success(200,"success",deliveryOrder);
    }
    @PostMapping("/updateDeliveryOrder")
    public Result updateDeliveryOrder(@RequestBody DeliveryOrder deliveryOrder){
        deliveryOrderService.updateById(deliveryOrder);
        return Result.success(200,"success",deliveryOrder);
    }
}
